using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace HOPE
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class HopeGame : Microsoft.Xna.Framework.Game
    {
        #region Fields

        GraphicsDeviceManager graphics;
        ScreenManager screenManager;

        public ContentManager StaticContent { get; set; }

        //global content.
        private SpriteFont hudFont;

        private Texture2D pauseOverlay;

        //meta-level game state
        private int levelIndex = -1;
        private Level level;
        private bool wasContinuedPressed;

        // When the time remaining is less than the warning time, it blinks on the hud
        //private static readonly TimeSpan WarningTime = TimeSpan.FromSeconds(30);

        // We store our input states so that we only poll once per frame, 
        // then we use the same input state wherever needed

        private GamePadState gamePadState;
        private KeyboardState keyboardState;
        private TouchCollection touchState;
        private AccelerometerState accelerometerState;

        // The number of levels in the Levels directory of our content. We assume that
        // levels in our content are 0-based and that all numbers under this constant
        // have a level file present. This allows us to not need to check for the file
        // or handle exceptions, both of which can add unnecessary time to level loading.
        private const int numberOfLevels = 3;

        //-------------------------------------------------------------------------
        //3. Add fields to store Texture and position of Virtual Thumbsticks.

        // a texture used to draw the Jump button on the screen
        public static Texture2D jumpButton;
        public static Vector2 jumpButtonPosition;
        public static bool isJumpButtonClicked;

        // a texture used to draw the Action button on the screen
        public static Texture2D actionButton;
        public static Vector2 actionButtonPosition;
        public static bool isActionButtonClicked;

        // a texture used to draw the virtual thumbsticks on the screen
        public static Texture2D controlstickBoundary;
        public static Texture2D controlstick;
        public static Vector2 controlstickStartupPosition;
        public static Vector2 controlstickBoundaryPosition;

        //add two viewport fields
        Viewport viewportHUD;
        Viewport viewportGameplay;

        #endregion

        #region Initialization

        /// <summary>
        /// The main game constructor.
        /// </summary>
        public HopeGame()
        {
            graphics = new GraphicsDeviceManager(this);
            StaticContent = new ContentManager(this.Services);

            graphics.IsFullScreen = true;
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            //Accelerometer.Initialize();

            Content.RootDirectory = "Content";
            StaticContent.RootDirectory = "Content";

            // you can choose whether you want a landscape or portait
            // game by using one of the two helper functions.
            //InitializePortraitGraphics();
            InitializeLandscapeGraphics();

            // Create the screen manager component.
            screenManager = new ScreenManager(this);

            Components.Add(screenManager);

            // attempt to deserialize the screen manager from disk. if that
            // fails, we add our default screens.
            if (!screenManager.DeserializeState())
            {
                // Activate the first screens.
                screenManager.AddScreen(new SplashScreen(), null);
                //screenManager.AddScreen(new BackgroundScreen(), null);
                //screenManager.AddScreen(new MainMenuScreen(), null);
            }
        }

        protected override void OnExiting(object sender, System.EventArgs args)
        {
            // serialize the screen manager whenever the game exits
            screenManager.SerializeState();

            base.OnExiting(sender, args);
        }



        /// <summary>
        /// Helper method to the initialize the game to be a portrait game.
        /// </summary>
        //private void InitializePortraitGraphics()
        //{
        //    graphics.PreferredBackBufferWidth = 480;
        //    graphics.PreferredBackBufferHeight = 800;
        //}

        /// <summary>
        /// Helper method to initialize the game to be a landscape game.
        /// </summary>
        private void InitializeLandscapeGraphics()
        {
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 480;
        }

        

        #endregion

        #region Draw

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Transparent);

            // The real drawing happens inside the screen manager component.
            base.Draw(gameTime);
        }

        #endregion
    }

    #region Entry Point

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static class Program
    {
        static void Main()
        {
            using (HopeGame game = new HopeGame())
            {
                game.Run();
            }
        }
    }

    #endregion
}
